//********************************usermanagement module*********************

/*
 * uses button display a DOM object or reverse. And update button text at the same time
 * obj: ID of DOM object
 * buttonName: button switcher name
 * textBefore: text of button before change
 * textAfter: text of button after change
 */
function toggleVisibility(obj, buttonName, textBefore, textAfter) {
	var el = $('#'+obj);
	var button = "input[name=" + buttonName + "]";
	if (!el.hasClass('displayNone')) {
		el.removeClass('displayBlock').addClass('displayNone');
		$(button).attr('value', textBefore);
	} else {
		el.removeClass('displayNone').addClass('displayBlock');
		$(button).attr('value', textAfter);
	}
}

/*
 * uses radio display a DOM object or reverse.
 * obj: ID of DOM object visible:
 * display or not
 */
function toggleVisibility(obj, visibility) {
	var el = $('#'+obj);
	if (visibility == 'true') {
		el.removeClass('displayNone').addClass('displayBlock');
	} else {
		el.removeClass('displayBlock').addClass('displayNone');
	}
}

/*
 * load available service agreements in ajax way
 */
function populateAvailableSAReport(){
	// target url
	url = "./B2BEnroll_browseSA.action";
	// retrieve service agreement value by html element id
	sa = jQuery.trim(document.getElementById('serviceNumber').value);
	// validate service agreement
	if(sa == "") {
		sa = '%';
	}
	// load available service agreements and populate the return report
	$('div#saReport').load(url, {'serviceNumber':sa}, function(){
		if($('div#saReport').html().indexOf('j_username') > 1){
			window.location = "./login.do";
		}
	});
}

/*
 * load available accounts in ajax way
 */
function populateAvailableAccountReport(){
	// target url
	url = "./B2BEnroll_browseAccount.action";
	// retrieve account value by html element id
	acct = jQuery.trim(document.getElementById('accountNumber').value);
	// validate account
	if(acct == "") {
		acct = '%';
	}
	// load available accounts and populate the return report
	$('div#accountReport').load(url, {'accountNumber':acct}, function(){
		if($('div#accountReport').html().indexOf('j_username') > 1){
			window.location = "./login.do";
		}
	});
}

/*
 * remove nested forms for IE
 */
function removeNestedForms(wrapperForm){
	// IE will crash if submitting nested form, so remove nested here
	if($.browser.msie){
		nestedForms = "#" + wrapperForm.id + " form";
		$(nestedForms).each(function(){
			$(this).replaceWith($(this).html());
		});
	}
}

/* js functions for validation of assigning SA and Acct. form submitting
function validateSAAssigned(){
	if(document.forms['enrollB2BForm'].assignSAFlag[0].checked){
		chosen = "";
		if(document.forms['enrollB2BForm'].cell_radio_0.checked){
			chosen = document.forms['enrollB2BForm'].cell_radio_0.value;
		}
		else{
			len = document.forms['enrollB2BForm'].cell_radio_0.length;
			for (i = 0; i <len; i++) {
				if (document.forms['enrollB2BForm'].cell_radio_0[i].checked) {
					chosen = document.forms['enrollB2BForm'].cell_radio_0[i].value;
				}
			}
		}
		if (chosen == "") {
			showError(notSelectAssignedSA);
			return false;
		}
		else {
			document.forms['enrollB2BForm'].submit();
		}
	}
	else if(document.forms['enrollB2BForm'].assignSAFlag[1].checked){
		document.forms['enrollB2BForm'].submit();
	}
	else{
		showError(notSelectYesNo);
		return false;
	}
}

function validateAccountAssigned(){
	if(document.forms['enrollB2BForm'].assignAccountFlag[0].checked){
		chosen = "";
		if(document.forms['enrollB2BForm'].cell_radio_0.checked){
			chosen = document.forms['enrollB2BForm'].cell_radio_0.value;
		}
		else{
			len = document.forms['enrollB2BForm'].cell_radio_0.length;
			for (i = 0; i <len; i++) {
				if (document.forms['enrollB2BForm'].cell_radio_0[i].checked) {
					chosen = document.forms['enrollB2BForm'].cell_radio_0[i].value;
				}
			}
		}
		if (chosen == "") {
			showError(notSelectAssignedAccount);
			return false;
		}
		else {
			document.forms['enrollB2BForm'].submit();
		}
	}
	else if(document.forms['enrollB2BForm'].assignAccountFlag[1].checked){
		document.forms['enrollB2BForm'].submit();
	}
	else{
		showError(notSelectYesNo);
		return false;
	}
}
*/